﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller.Enum;
using Model;

namespace SalePriceSystem.Page
{
    public partial class ImageUpload : System.Web.UI.Page
    {
        private string UserName = string.Empty;
        private List<tblUser> us;
        protected void Page_Load(object sender, EventArgs e)
        {
            UserName = HttpContext.Current.Request.QueryString["u"];
            us = Controller.Helper.Member.GetUserDB(UserName);
            if (us.Count > 0)
            {
                ImagePreview.ImageUrl = us[0].ImageLicenseRelative;
            }
        }

        protected void ButtonUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                UserName = HttpContext.Current.Request.QueryString["u"];
                string pathsave = "/Upload/" + UserName ?? "default";
                pathsave += "/";
                string folderpath = System.Web.HttpContext.Current.Server.MapPath(pathsave);
                if (!System.IO.Directory.Exists(folderpath))
                {
                    System.IO.Directory.CreateDirectory(folderpath);
                }
                string imagePath = folderpath + FileUpload1.FileName;
                FileUpload1.SaveAs(imagePath);
                try
                {
                    
                    if (us.Count > 0)
                    {
                        us[0].ImageLicense = imagePath;
                        us[0].ImageLicenseRelative = pathsave + FileUpload1.FileName;
                    }
                    int round = Controller.Helper.Member.UserAction(us, EnumAction.Update);
                    LiteralReport.Text = string.Format("Upload success");
                    ImagePreview.ImageUrl = us[0].ImageLicenseRelative;
                    btnCloseButton.Visible = true;
                    ButtonUpload.Visible = false;
                }
                    
                catch (Exception)
                {

                    LiteralReport.Text = string.Format("Import not success please contact your administrator");
                }

            }
            else
            {
                LiteralReport.Text = string.Format("Please upload the correct file");
            }
        }
    }
}